package com.example.travel.controller;

import com.example.travel.pojo.Seller;
import com.example.travel.service.SellerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("admin/seller")
public class SellerController {

    @Autowired
    private SellerService sellerService;

    //多条件分页查询
    @RequestMapping("page")
    public String SellerList(//一定要设置默认值，不然参数为空会报错
                             @RequestParam(defaultValue = "1") int pageNum,
                             @RequestParam(defaultValue = "5") int pageSize,
                             String sName,
                             String address,
                             Model model){

        Seller seller = new Seller();

        //设置查询条件
        if (sName !=null){
            seller.setSName(sName.trim());
        }

        if (address !=null){
            seller.setAddress(address.trim());
        }
        //存入查询条件
        model.addAttribute("condition",seller);

        PageInfo<Seller> sellerPageInfo = sellerService.PageInfoFindByParam(pageNum, pageSize, seller);

        model.addAttribute("page",sellerPageInfo);

        return "seller/list";
    }

    //新增旅游公司
    @RequestMapping("add")
    public String add(@RequestParam("sname") String sName,
                      @RequestParam("consphone") String consPhone,
                      @RequestParam("address") String address){

        Seller seller = new Seller();
        seller.setSName(sName.trim());
        seller.setConsPhone(consPhone.trim());
        seller.setAddress(address.trim());

        int flag = sellerService.addSeller(seller);

        if (flag >=1){
            return "redirect:page";
        }else {
            return "error";
        }
    }

    //获取原数据信息
    @RequestMapping("getInfo/{sid}")
    public String getSellerInfo(@PathVariable("sid") Integer sId,
                                Model model){

        Seller seller = sellerService.getInfo(sId);

        model.addAttribute("seller",seller);

        return "/seller/edit";
    }

    //更新数据
    @RequestMapping("update")
    public String update(@RequestParam("sid") Integer sId,
                         @RequestParam("sname") String sName,
                         @RequestParam("consphone") String consPhone,
                         @RequestParam("address") String address){

        Seller seller = new Seller();
        seller.setSid(sId);
        seller.setSName(sName);
        seller.setConsPhone(consPhone);
        seller.setAddress(address);

        int flag = sellerService.update(seller);
        if (flag >=1){
            return "redirect:page";
        }else {
            return "error";
        }
    }

    //删除
    @RequestMapping("delete/{sid}")
    public int deleteSeller(@PathVariable("sid") Integer sid){

        return sellerService.deleteSeller(sid);
    }

}
